
package me.zhengjie.modules.summary.service.impl;

import me.zhengjie.modules.summary.domain.RecordSub;
import me.zhengjie.modules.summary.domain.RecordYearSub;
import me.zhengjie.modules.summary.repository.RecordYearSubRepository;
import me.zhengjie.modules.summary.service.RecordYearSubService;
import me.zhengjie.modules.summary.service.dto.RecordYearSubDto;
import me.zhengjie.modules.summary.service.dto.RecordYearSubQueryCriteria;
import me.zhengjie.modules.summary.service.mapstruct.RecordYearSubMapper;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.summary.repository.RecordSubRepository;
import me.zhengjie.modules.summary.service.RecordSubService;
import me.zhengjie.modules.summary.service.dto.RecordSubDto;
import me.zhengjie.modules.summary.service.dto.RecordSubQueryCriteria;
import me.zhengjie.modules.summary.service.mapstruct.RecordSubMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author yyj
* @date 2022-12-26
**/
@Service
@RequiredArgsConstructor
public class RecordYearSubServiceImpl implements RecordYearSubService {

    private final RecordYearSubRepository recordYearSubRepository;
    private final RecordYearSubMapper recordYearSubMapper;

    @Override
    public Map<String,Object> queryAll(RecordYearSubQueryCriteria criteria, Pageable pageable){
        Page<RecordYearSub> page =  recordYearSubRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(recordYearSubMapper::toDto));
    }

    @Override
    public List<RecordYearSubDto> queryAll(RecordYearSubQueryCriteria criteria){
        return recordYearSubMapper.toDto(recordYearSubRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RecordYearSubDto findById(String id) {
        RecordYearSub recordYearSub = recordYearSubRepository.findById(id).orElseGet(RecordYearSub::new);
        ValidationUtil.isNull(recordYearSub.getId(),"RecordSub","id",id);
        return recordYearSubMapper.toDto(recordYearSub);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RecordYearSubDto create(RecordYearSub resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return recordYearSubMapper.toDto(recordYearSubRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RecordYearSub resources) {
        RecordYearSub recordYearSub = recordYearSubRepository.findById(resources.getId()).orElseGet(RecordYearSub::new);
        ValidationUtil.isNull( recordYearSub.getId(),"RecordYearSub","id",resources.getId());
        recordYearSub.copy(resources);
        recordYearSubRepository.save(recordYearSub);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            recordYearSubRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<RecordYearSubDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RecordYearSubDto recordSub : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", recordSub.getName());
            map.put("关联记录表id", recordSub.getRecordId());
            map.put("创建时间", recordSub.getCreateTime());
            map.put("更新时间", recordSub.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}